/*
 *  Copyright 2008-2010 www.jrails.org
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.rails.extjs;

import java.util.ArrayList;
import java.util.List;

import org.rails.extjs.layout.Layout;

public abstract class Container extends BoxComponent {
	
	protected Layout layout;
	
	public static final String LAYOUT_ABSOLUTE = "absolute";
	public static final String LAYOUT_ACCORDION = "accordion";
	public static final String LAYOUT_ANCHOR = "anchor";
	public static final String LAYOUT_AUTO = "auto";
	public static final String LAYOUT_BORDER = "border";
	public static final String LAYOUT_CARD = "card";
	public static final String LAYOUT_COLUMN = "column";
	public static final String LAYOUT_FIT = "fit";
	public static final String LAYOUT_FORM = "form";
	public static final String LAYOUT_HBOX = "hbox";
	public static final String LAYOUT_MENU = "menu";
	public static final String LAYOUT_TABLE = "table";
	public static final String LAYOUT_TOOLBAR = "toolbar";
	public static final String LAYOUT_VBOX = "vbox";

	protected List<Component> items = new ArrayList<Component>();
	
	public Container() {
		super();
	}
	
	public Container(Option configOptions) {
		super(configOptions);
	}

	public Container(String id) {
		super(id);
	}
	
	@Override
	protected void createItems(){
		super.createItems();
		if(items.size() > 0)
			addOption("items",items);
	}

	public void setActiveItem(Object activeItem){
		addOption("activeItem",activeItem);
	}

	
	public Object getActiveItem(){
		return getOption("activeItem");
	}

	public void setAutoDestroy(Boolean autoDestroy){
		addOption("autoDestroy",autoDestroy);
	}
	
	public Boolean isAutoDestroy(){
		return getBoolean("autoDestroy",true);
	}
	
	public void setBufferResize(Object bufferResize){
		addOption("bufferResize",bufferResize);
	}
	
	public Object getBufferResize(){
		return getOption("bufferResize");
	}
	
	public void setDefaults(Option defaults){
		addOption("defaults",defaults);
	}
	
	public Option getDefaults(){
		return (Option)getOption("defaults");
	}
	
	public void setForceLayout(Boolean forceLayout){
		addOption("forceLayout",forceLayout);
	}
	
	public Boolean isForceLayout(){
		return getBoolean("forceLayout",false);
	}
	
	public void setHideBorders(Boolean hideBorders){
		addOption("hideBorders",hideBorders);
	}
	
	public Boolean isHideBorders(){
		return getBoolean("hideBorders",false);
	}
	
	public void setLayout(Layout layout){
		this.layout = layout;
		addOption("layout", layout.getLayout());
		if(!layout.getOption().equals(new Option()))
			addOption("layoutConfig", layout.getOption());
	}
	
	public Layout getLayout(){
		return layout;
	}
	
	public Option getLayoutConfig(){
		return (Option)getOption("layoutConfig");
	}
	
	public void insertItem(int index,Component component){
		items.add(index, component);
	}

	public void addItem(Component component){
		items.add(component);		
	}
	
	public List<Component> getItems(){
		return items;
	}
	
	public void removeItem(int index){
		items.remove(index);
	}
	
	public Component getItem(int index){
		return items.get(index);
	}
	
	public void clearItems(){
		items.clear();
	}
}
